package com.wn.quality.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wn.common.PageBean;
import com.wn.entity.IncomingInspection;
import com.wn.entity.InventoryBookkeeping;
import com.wn.quality.mapper.IncomingInspectionMapper;
import com.wn.quality.mapper.InventoryBookkeepingMapper;
import com.wn.quality.service.IncomingInspectionService;
import com.wn.utils.PageBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wuqingting
 * @date 2022/10/24
 */
@Service
public class IncomingInspectionServiceImpl implements IncomingInspectionService {
    @Autowired
    private IncomingInspectionMapper incomingInspectionMapper;
    @Autowired
    private InventoryBookkeepingMapper inventoryBookkeepingMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(IncomingInspection incomingInspection) {
        incomingInspectionMapper.add(incomingInspection);
        incomingInspection.getInventoryBookkeeping().setState("检验中");
        inventoryBookkeepingMapper.update(incomingInspection.getInventoryBookkeeping());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void complete(IncomingInspection incomingInspection) {
        incomingInspectionMapper.update(incomingInspection);
        inventoryBookkeepingMapper.update(incomingInspection.getInventoryBookkeeping());
    }

    @Override
    public IncomingInspection getById(int id) {
        return incomingInspectionMapper.getById(id);
    }

    @Override
    public PageBean<IncomingInspection> getPageByCondition(IncomingInspection incomingInspection,int currPage,int pageSize) {
        Page<Object> page = PageHelper.startPage(currPage, pageSize);
        List<IncomingInspection> list = incomingInspectionMapper.getByCondition(incomingInspection);
        PageBean<IncomingInspection> pageBean = PageBeanUtil.getPageBean(list, page, currPage, pageSize);

        return pageBean;
    }

    @Override
    public List<IncomingInspection> getAllByCondition(IncomingInspection incomingInspection) {
        return incomingInspectionMapper.getByCondition(incomingInspection);
    }

    @Override
    public PageBean<InventoryBookkeeping> getInventoryByPage(String state,int currPage,int pageSize) {
        Page<Object> page = PageHelper.startPage(currPage, pageSize);
        List<InventoryBookkeeping> bookkeepingList = inventoryBookkeepingMapper.getByState(state);
        PageBean<InventoryBookkeeping> pageBean = PageBeanUtil.getPageBean(bookkeepingList, page, currPage, pageSize);

        return pageBean;
    }


}
